use actix_cors::Cors;
use actix_web::{App, HttpServer, web};
use crate::common::global_config::AppState;
use crate::common::init_surrealdb::init_db;

mod user;
mod demo;

pub async fn start_web() -> std::io::Result<()> {
    let db = init_db().await;
    let app_state = web::Data::new(AppState::new_with_db(db));

    HttpServer::new(move || {
        App::new()
            .wrap(Cors::permissive())
            .app_data(app_state.clone())
            .configure(move |cfg| {
                demo::router_config(cfg);
                user::router_config(cfg);
            })
    })
        .bind(("0.0.0.0", 8080))?
        .run()
        .await
}

